package com.ufcg.lvc.client.prototipacao;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.ufcg.lvc.client.GreetingServiceCRUD;
import com.ufcg.lvc.client.GreetingServiceCRUDAsync;
import com.ufcg.lvc.client.UserData;

public class ResultadoProvas {

	AbsolutePanel panelQuestoes;
	String nome_, texto;
	ListGrid listGrid;
	ListBox listBox_1;
	Double somaNotas;
	TextBox somaTB;
	ArrayList<String> alunos;
	ArrayList<String> notas;
	UserData UD;
	
	public static GreetingServiceCRUDAsync getServicoCRUD() {
		return GWT.create(GreetingServiceCRUD.class);
	}

	public ResultadoProvas(String nome, String disciplina, String instituicao,
			String ano,UserData uD) {
		UD = uD;
		nome_ = nome;
		panelQuestoes = new AbsolutePanel();
		panelQuestoes.setSize("900px", "707px");

		Label titulo = new Label(nome);
		titulo.setStyleName("gwt-TituloMedio");
		panelQuestoes.add(titulo, 0, 10);

		listGrid = new ListGrid();
		panelQuestoes.add(listGrid, 60, 102);
		listGrid.setSize("452px", "477px");

		listBox_1 = new ListBox();
		panelQuestoes.add(listBox_1, 107, 69);
		listBox_1.setSize("405px", "25px");
		listBox_1.setVisibleItemCount(1);
		listBox_1.setSelectedIndex(0);

		somaTB = new TextBox();
		somaTB.setText("00.00");
		somaTB.setEnabled(false);
		panelQuestoes.add(somaTB, 168, 585);
		somaTB.setSize("43px", "17px");

		ListGridField field0 = new ListGridField("aluno", "Aluno");
		field0.setCanEdit(false);

		ListGridField field1 = new ListGridField("nota", "Nota");
		field1.setCanEdit(false);

		listGrid.setFields(field0, field1);
		listGrid.setCanEdit(false);

		getServicoCRUD().getProvasSubtitulo(nome, UD.getId_usuario(), new AsyncCallback<List<String>>() {
					@Override
					public void onSuccess(List<String> result) {
						for (int j = 0; j < result.size(); j++) {
							listBox_1.addItem(result.get(j));
						}
					}
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Ocorreu um erro servidor. Tente novamente recarregar a página.");
					}
				});

		listBox_1.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (listBox_1.getSelectedIndex() > -1){
				getServicoCRUD().getProvasNotas(nome_,listBox_1.getItemText(listBox_1.getSelectedIndex()), UD.getId_usuario(), new AsyncCallback<ArrayList<ArrayList<String>>>() {

						@Override
						public void onSuccess(ArrayList<ArrayList<String>> result) {
							somaNotas = 0.0;
							alunos = new ArrayList<String>();
							notas = new ArrayList<String>();

							alunos = result.get(0);
							notas = result.get(1);
							
							for (int i = 0; i < alunos.size(); i++) {
								somaNotas += Double.parseDouble(notas.get(i));
								somaTB.setText(somaNotas / alunos.size()+ "");
							}
							if (alunos.size() > 0) {
								ListGridRecord[] qRecord = new ListGridRecord[alunos.size()];
								for (int i = 0; i < alunos.size(); i++) {
									qRecord[i] = new NameValueRecord(alunos.get(i), notas.get(i));
								}
								listGrid.setData(qRecord);
								listGrid.redraw();
							}
						}

						@Override
						public void onFailure(Throwable caught) {
							System.out.println("deu erro aqui!");
						}
					});
				}
			}
		});

		Button btnVoltar = new Button(	"Voltar");
		panelQuestoes.add(btnVoltar, 454, 657);
		btnVoltar.setSize("105px", "29px");
		btnVoltar.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				voltar();
			}
		});

		Label lblMdia = new Label("Média da prova:");
		panelQuestoes.add(lblMdia, 60, 589);

		Label lblProva = new Label("Prova:");
		panelQuestoes.add(lblProva, 60, 69);

		Label lblPontos = new Label("pontos");
		panelQuestoes.add(lblPontos, 225, 589);

		Button btnImprimir = new Button("Imprimir");
		panelQuestoes.add(btnImprimir, 423, 585);
		btnImprimir.setSize("89px", "29px");
		
		btnImprimir.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if (listBox_1.getSelectedIndex() > -1) {
					texto = (listBox_1.getItemText(listBox_1.getSelectedIndex()) + "\r\n\r\n");
					for (int i = 0; i < alunos.size(); i++) {
						texto += alunos.get(i) + " - " + notas.get(i) + "\r\n";
					}
					
					getServicoCRUD().gravarResultados(listBox_1.getItemText(listBox_1.getSelectedIndex()), texto, UD.getLogin_usuario(), new AsyncCallback<String>() {
						
						@Override
						public void onSuccess(String result) {
							Window.open(GWT.getHostPageBaseURL()+ result, "_blank", "enabled");
							voltar();
						}
						
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Erro ao gerar link para download. Tente novamente mais tarde.");
							voltar();
						}
					});
				}
			}
		});
	}
//
//	private ListGridRecord[] getRecord() {
//		ListGridRecord[] qRecord = new ListGridRecord[alunos.size()];
//		for (int i = 0; i < alunos.size(); i++) {
//			qRecord[i] = new NameValueRecord(alunos.get(i), notas.get(i));
//		}
//		return qRecord;
//	}

	public AbsolutePanel getPanel() {
		return panelQuestoes;
	}
	
	public void voltar(){
		panelQuestoes.clear();
		MinhasTurmas m = new MinhasTurmas(UD);
		panelQuestoes.add(m.getPanel());
	}

	/**
	 */
	public static class NameValueRecord extends ListGridRecord {
		String encontrada, esperada;
		double valorMaximo, valorQuestao;

		public NameValueRecord(String alunoNome, String alunoNota) {
			setNota(alunoNota);
			setAluno(alunoNome);
		}

		public String getAluno() {
			return getAttribute("aluno");
		}

		public String getNota() {
			return getAttribute("nota");
		}

		public void setNota(String numero) {
			setAttribute("nota", numero);
		}

		public void setAluno(String esperada) {
			setAttribute("aluno", esperada);
		}
	}
}
